#include <cmath>
#include "vec3.h"


vec3::vec3(float nx, float ny, float nz)
{
	x = nx;
	y = ny;
	z = nz;
}

vec3::~vec3(void)
{
}

// returns the vector's squared magnitude
float vec3::sqmag() {
	return dot(this);
}

// returns the squared dot product of two vectors
float vec3::dot(vec3* other) {
	return this->x*other->x + this->y*other->y + this->z*other->z;
}

// returns the vector's squared magnitude
float vec3::mag() {
	return  sqrt(sqmag());
}

// scale the vector
void vec3::scale (float scalar) {
	x *= scalar;
	y *= scalar;
	z *= scalar;
}

// returns the squared dot product of two vectors
void vec3::normalise() {
	float len = mag(); // get the magnitude
	if (len > 0.000001f) // make sure its worthwhile
		scale (1.0f / len); // scale by the inverse of the magnitude
}


///////////////////////////////////////
//			+ Operators				 //
///////////////////////////////////////

vec3 vec3::operator+ (const vec3& c) {
	return vec3 (x + c.x, y + c.y, z + c.z);
}

vec3 vec3::operator+ (const float& c){
	return vec3(x + c, y + c, z + c);
}

void vec3::operator+= (const vec3& c){
	x += c.x;
	y += c.y;
	z += c.z;
}

void vec3::operator+= (const float& c){
	x += c;
	y += c;
	z += c;
}

///////////////////////////////////////
//			- Operators				 //
///////////////////////////////////////

vec3 vec3::operator- (const vec3& c) {
	return vec3 (x - c.x, y - c.y, z -c.z);
}

vec3 vec3::operator- (const float& c){
	return vec3(x - c, y - c, z - c);
}

void vec3::operator-= (const vec3& c){
	x -= c.x;
	y -= c.y;
	z -= c.z;
}

void vec3::operator-= (const float& c){
	x -= c;
	y -= c;
	z -= c;
}

///////////////////////////////////////
//			* Operators				 //
///////////////////////////////////////

vec3 vec3::operator* (const vec3& c) {
	return vec3 (x * c.x, y * c.y, z * c.z);
}

vec3 vec3::operator* (const float& c){
	return vec3 (x * c, y * c, z *c);
}

void vec3::operator*= (const vec3& c){
	x *= c.x;
	y *= c.y;
	z *= c.z;
}

void vec3::operator*= (const float& c){
	x *= c;
	y *= c;
	z *= c;
}

///////////////////////////////////////
//			/ Operators				 //
///////////////////////////////////////

vec3 vec3::operator/ (const vec3& c) {
	return vec3 (x / c.x, y / c.y, z /c.z);
}

vec3 vec3::operator/ (const float& c){
	return vec3(x / c, y / c, z / c);
}

void vec3::operator/= (const vec3& c){
	x /= c.x;
	y /= c.y;
	z /= c.z;
}

void vec3::operator/= (const float& c){
	x /= c;
	y /= c;
	z /= c;
}


